const { courseModel, openedModel,workTeaModel, workPubModel } = require('../../model');
const { getResultToObjectComplete } = require('../../services/utils');

module.exports = {
  getOrgWorksList: async (req,res) => {
    const { org } = req.decode;
    try {
      const result = await workPubModel.findByFilter(['id','pageURL','name'], {org: org});
      return res.send({
        code: 20000,
        data: result
      });
    } catch (e) {
      console.log(e);
    }
  },
  getMyWorksList: async (req,res) => {
    const { id, org } = req.decode;
    try {
      const teach_works = await openedModel.findByRelation(['course_id', 'createdAt', 'updatedAt'],{ stu_id: id },{
        model: workTeaModel.getModel(),
        attributes: ['id','pageURL','name']
      });
      const teach_works_list = getResultToObjectComplete(teach_works, 'works_teach', ['id','pageURL','name']);
      const public_works = await workPubModel.findByFilter(['id','name','pageURL','createdAt','updatedAt'],{ stu_id: id, isOpened: 0});
      const pageList = teach_works_list.filter(item => new Date(item.createdAt).getTime() !== new Date(item.updatedAt).getTime());

      return res.send({
        code: 20000,
        data: pageList.concat(public_works)
      });
    } catch (e) {
      console.log(e);
    }
  }
};
